package com.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 149. 直线上最多的点数
 *
 * @author fy
 * @date 2022/4/8 20:32
 */
public class Solution149_1 {

    public int maxPoints(int[][] points) {
        int n = points.length;
        if (n <= 2) {
            return n;
        }
        int ret = 0;
        for (int i = 0; i < n; i++) {
            if (ret >= n - i || ret > n / 2) {
                break;
            }
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int j = i + 1; j < n; j++) {
                int x = points[i][0] - points[j][0];
                int y = points[i][1] - points[j][1];
                if (x == 0) {
                    y = 1;
                } else if (y == 0) {
                    x = 1;
                } else {
                    if (y < 0) {
                        x = -x;
                        y = -y;
                    }
                    int gcdXY = gcd(Math.abs(x), Math.abs(y));
                    x /= gcdXY;
                    y /= gcdXY;
                }
                int key = y + x * 20001;
                map.put(key, map.getOrDefault(key, 0) + 1);
            }
            int maxn = 0;
            for (Map.Entry<Integer, Integer> entry: map.entrySet()) {
                int num = entry.getValue();
                maxn = Math.max(maxn, num + 1);
            }
            ret = Math.max(ret, maxn);
        }
        return ret;
    }

    public int gcd(int a, int b) {
        return b != 0 ? gcd(b, a % b) : a;
    }

    public static void main(String[] args) {
        int[][] points = new int[][]
                {
                        {54, 153},
                        {1, 3},
                        {0, -72},
                        {-3, 3},
                        {12, -22},
                        {-60, -322},
                        {0, -5},
                        {-5, 1},
                        {5, 5},
                        {36, 78},
                        {3, -4},
                        {5, 0},
                        {0, 4},
                        {-30, -197},
                        {-5, 0},
                        {60, 178},
                        {0, 0},
                        {30, 53},
                        {24, 28},
                        {4, 5},
                        {2, -2},
                        {-18, -147},
                        {-24, -172},
                        {-36, -222},
                        {-42, -247},
                        {2, 3},
                        {-12, -122},
                        {-54, -297},
                        {6, -47},
                        {-5, 3},
                        {-48, -272},
                        {-4, -2},
                        {3, -2},
                        {0, 2},
                        {48, 128},
                        {4, 3},
                        {2, 4}
                };

        int res = new Solution149_1().maxPoints(points);
        System.out.println(res);
    }

}
